package com.zhangtai.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.entity.common.NoticeEntity;
import com.zhangtai.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: ming
 * @Date: 2020/6/15 0015 上午 8:39
 */
@RestController
@Slf4j
@RequestMapping("err")
public class SendErr {
    private static final String url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wxf9cc53004252c251&corpsecret=zYfGMAWgn5T8Fh7sjqJ318Wv2JMKz5LVzpoWeWcDsqs";
    private String sengUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
    private static final Integer agentid=1000063;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;


    @Scheduled(cron="0 0 0/1 * * ?")
    @RequestMapping("/getQWtoken")
    public String getToken(){
        log.info("开始刷新企微token");
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        String str = forEntity.getBody();
        JSONObject jsonObject = JSON.parseObject(str, JSONObject.class);
        String access_token = jsonObject.getString("access_token");
        redisTemplate.boundValueOps("QW_TOKEN").set(access_token);
        return access_token;
    }

    @GetMapping("/Exception")
    public void send(HttpServletRequest request,Exception e){



        String token = (String) redisTemplate.boundValueOps("QW_TOKEN").get();
        log.info("企微token为:{}",token);
        if (StringUtils.isEmpty(token)){
            token = getToken();
        }

        String format = String.format(sengUrl, token);
        JSONObject object = new JSONObject();
        object.put("touser","15578918857|13407860527|18677389714|15814635200|13246795761|13297461304|18377177242");
        object.put("msgtype","text");
        object.put("agentid",agentid);
        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        jsonObject.put("content",time+","+request.getRequestURL()+","+e.getMessage()+"\n"+e.toString());
        object.put("text",jsonObject);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(object,headers);

        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(format, httpEntity, JSONObject.class);
        JSONObject body = responseEntity.getBody();
        log.info("推送结果：{}",body.toString());
    }


    public void sendMsg(String s){



        String token = (String) redisTemplate.boundValueOps("QW_TOKEN").get();
        log.info("企微token为:{}",token);
        if (StringUtils.isEmpty(token)){
            token = getToken();
        }

        String format = String.format(sengUrl, token);
        JSONObject object = new JSONObject();
        object.put("touser","15578918857|13407860527|18677389714|15814635200");
        object.put("msgtype","text");
        object.put("agentid",agentid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content",s);
        object.put("text",jsonObject);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(object,headers);

        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(format, httpEntity, JSONObject.class);
        JSONObject body = responseEntity.getBody();
        log.info("推送结果：{}",body.toString());
    }


    public void sendErr(Exception e){



        String token = (String) redisTemplate.boundValueOps("QW_TOKEN").get();
        log.info("企微token为:{}",token);
        if (StringUtils.isEmpty(token)){
            token = getToken();
        }

        String format = String.format(sengUrl, token);
        JSONObject object = new JSONObject();
        object.put("touser","15578918857|13407860527|18677389714|15814635200");
        object.put("msgtype","text");
        object.put("agentid",agentid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content","mq消费者出错"+"\n"+e.toString());
        object.put("text",jsonObject);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(object,headers);

        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(format, httpEntity, JSONObject.class);
        JSONObject body = responseEntity.getBody();
        log.info("推送结果：{}",body.toString());
    }


}
